package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;
    /*
    1.添加菜品
    2.添加口味表
     */
    @Transactional
    @Override
    public void saveDishAndFlavors(DishDto dishDto) {
        //1.添加菜品
        //这个保存操作，只能保存菜品，口味无法保存。
        System.out.println(dishDto.getId());//null
        super.save(dishDto);//dishDto保存之后，mybatisplus会自动将数据库中的主键设置到dishDto的id属性中
        System.out.println(dishDto.getId());//有id
        //2.添加口味表
        //为每个口味，建立与菜品的关联，dishId字段
        /*for (DishFlavor flavor : dishDto.getFlavors()) {
            flavor.setDishId(dishDto.getId());
        }*/
        List<DishFlavor> list = dishDto.getFlavors().stream().map(f -> {
            f.setDishId(dishDto.getId());
            return f;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(list);
    }
    /*
        菜品分页查询，并且还要查询每个菜品的分类名称
     */
    @Override
    public Page<DishDto> pageDishAndCategoryName(Integer page, Integer pageSize, String name) {
        //1.查询菜品的分页数据
        Page<Dish> dishPage = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        dishLambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
        super.page(dishPage,dishLambdaQueryWrapper);
        //2.根据每个菜品的分类id查询对应的分类名称，并封装成dishdto集合
        /*
            把dishpage中除了records以外的其他属性，都赋值给dishdtopage
         */
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        //把dish集合收集成stream流
        List<DishDto> dishDtoList = dishPage.getRecords().stream().map(dish -> {
            //遍历流中所有dish对象，把每个dish对象都转成一个对应dishdto对象
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            //缺少分类名称属性，所以再根据categoryId查询对应的分类对象
            Category category = categoryService.getById(dish.getCategoryId());
            //把分类名称设置到dishdto中
            dishDto.setCategoryName(category.getName());
            //取出来的是dish，放回去的是dishdto
            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(dishDtoList);
        return dishDtoPage;
    }

    /*
        修改菜品回显功能
        查询dish表
        查询flavor口味表
        将dish数据和flavor集合数据封装成dishDto，再返回
     */
    @Override
    public DishDto findDishDtoById(Long id) {
        Dish dish = super.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //手动查询口味集合
        //select * from dish_flavor where dish_id= #{id};
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> list = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
        dishDto.setFlavors(list);

        return dishDto;
    }

    /*
        根据传入的dishdto修改 菜品和口味表
     */
    @Transactional
    @Override
    public void updateDishAndFlavor(DishDto dishDto) {
        //1.修改菜品表
        super.updateById(dishDto);
        //2.修改口味表
        //2.1 删除当前菜品的所有口味
        //delete from dish_flavor where dish_id = ?
        LambdaUpdateWrapper<DishFlavor> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(lambdaUpdateWrapper);
        //2.2 根据传入的口味集合，来重建口味

        List<DishFlavor> dishFlavors = dishDto.getFlavors().stream().map(f -> {
            f.setDishId(dishDto.getId());
            return f;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(dishFlavors);
    }

    @Override
    public List<DishDto> findDishDtoByCategoryId(Long categoryId, Integer status) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,categoryId);
        dishLambdaQueryWrapper.eq(status!=null,Dish::getStatus,status);
        dishLambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
        List<Dish> list = super.list(dishLambdaQueryWrapper);
        //dish集合转成dto集合
        List<DishDto> dishDtoList = list.stream().map(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);//还缺少口味集合
            //查询当前dish的口味集合
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
            //select * from dishFlavors where dish_id = ?
            List<DishFlavor> dishFlavors = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
            dishDto.setFlavors(dishFlavors);
            return dishDto;
        }).collect(Collectors.toList());

        return dishDtoList;
    }
}
